//
//  XTBSessionManager.m
//  Pods
//
//  Created by LiuYihua on 2017/5/10.
//
//

#import "XTBSessionManager.h"
#import "AFNetworking.h"

@interface XTBSessionManager()

@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;

@end

@implementation XTBSessionManager

- (void)setValue:(NSString *)value forHttpField:(NSString *)field {
    [self.sessionManager.requestSerializer setValue:value forHTTPHeaderField:field];
}

-(AFHTTPSessionManager *)sessionManager {
    if (!_sessionManager) {
        _sessionManager = [[AFHTTPSessionManager alloc] init];
        NSMutableSet *setM = [_sessionManager.responseSerializer.acceptableContentTypes mutableCopy];
        [setM addObject:@"text/plain"];
        [setM addObject:@"text/html"];
        _sessionManager.responseSerializer.acceptableContentTypes = [setM copy];
    }
    return _sessionManager;
}

- (void)request:(RequestType)requestType urlStr: (NSString *)urlStr parameter: (NSDictionary *)param resultBlock: (void(^)(id responseObject, NSError *error))resultBlock {
    
    
    void(^successBlock)(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        resultBlock(responseObject, nil);
    };
    
    void(^failBlock)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        resultBlock(nil, error);
    };
    
    if (requestType == RequestTypeGet) {
        [self.sessionManager GET:urlStr parameters:param progress:nil success:successBlock failure:failBlock];
    }else {
        [self.sessionManager POST:urlStr parameters:param progress:nil success:successBlock failure:failBlock];
    }
    
    
}
- (void)updateFileWithUrlStr: (NSString *)urlStr parameter: (NSDictionary *)param fileData:(NSData *)fileData filename:(NSString *)filename mimeType:(NSString *)mimeType resultBlock: (void(^)(id responseObject, NSError *error))resultBlock {
    
    /*
     第一个参数:请求路径
     第二个参数:非文件参数 username(参数名) (传NSDictionary)
     第三个参数:constructingBodyWithBlock 在该block中告诉AFN要上传的文件数据信息
     第四个参数:progress 进度回调
     第五个参数:success 成功后的回调
     第六个参数:failure 失败后的回调
     */
    [self.sessionManager POST:urlStr parameters:param constructingBodyWithBlock:
     ^(id<AFMultipartFormData>  _Nonnull formData) {
         /*
          第一个参数:文件的二进制文件
          第二个参数:参数名称 "file"
          第三个参数:在服务器上的名称
          第四个参数:文件的类型
          */
         
         [formData appendPartWithFileData:fileData name:@"file" fileName:filename mimeType:mimeType];
         
     } progress:^(NSProgress * _Nonnull uploadProgress) {
         
         //计算文件的上传进度
         NSLog(@"%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
         
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         
         //responseObject 响应体信息(默认已经完成了JSON的解析,是一个OC对象)
         resultBlock(responseObject,nil);
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         
         resultBlock(nil,error);
     }];
}

@end
